package dom;

import java.util.Calendar;
import java.util.Date;

public class BookReservation {

	//@Deprecated
	private StudentAccount studentAccount;
	private Book book;
	private Date dueDate;
	private Date reservationDate;
	//private int numDays;
	private double feesDue;
	
	private final int DEFAULT_RESERVATION_DAYS = 14;
	private final double LATE_FEE_PER_DAY = 1;
	
	public BookReservation(){
		
	}
	
	public BookReservation(Book book) throws IllegalArgumentException {
		if(book == null) {
			throw new IllegalArgumentException("The book cannot be null");
		}
		
		this.book = book;
		//this.numDays = DEFAULT_RESERVATION_DAYS;
		Calendar cal = Calendar.getInstance();
		this.reservationDate = cal.getTime();
		//
		Calendar cal2 = Calendar.getInstance();
		cal2.add(Calendar.DAY_OF_YEAR, DEFAULT_RESERVATION_DAYS);
		this.dueDate = cal2.getTime();
	}
	
	public BookReservation(Book book, int daysAgo) throws Exception, IllegalArgumentException {
		if(book == null) {
			throw new IllegalArgumentException("The book cannot be null");
		}
		if(book.getCopies() <= 0) {
			throw new Exception("There are no copies of the book " + book.getName() + " left.");
		}
		
		this.book = book;
		//this.numDays = DEFAULT_RESERVATION_DAYS;
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, -(daysAgo));
		this.dueDate = cal.getTime();
	}
	
	public Book getBook() {
		return book;
	}
	
	public void setBook(Book book) {
		this.book = book;
	}
	
	public Date getDueDate() {
		return dueDate;
	}
	
	public void setDueDate(Date dueDate) {
		this.dueDate = dueDate;
	}
	
	public Date getReservationDate() {
		return reservationDate;
	}
	
	public void setReservationDate(Date reservationDate) {
		this.reservationDate = reservationDate;
	}
	
/*	
	public int getNumDays() {
		return numDays;
	}
	
	public void setNumDays(int numDays) {
		this.numDays = numDays;
	}
*/	

	public double getFeesDue() {
		return feesDue;
	}

	public void setFeesDue(double feesDue) {
		this.feesDue = feesDue;
	}
	
	/**
	 * Returns the late fees based on the days overdue
	 * @return
	 */
	public double getLateFees(){
		return (getDaysOverdue() * LATE_FEE_PER_DAY);
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Book Reservation [");
		sb.append(book.getName() + ", " + book.getAuthor() + ", ");
		sb.append(dueDate.toString() + "]");
		return sb.toString();
	}

	public StudentAccount getStudentAccount() {
		return studentAccount;
	}

	public void setStudentAccount(StudentAccount studentAccount) {
		this.studentAccount = studentAccount;
	}

	/**
	 * Checks if the book reservation is overdue
	 * @param bookReservation
	 * @return
	 */
	public synchronized int getDaysOverdue(){
		
		int daysLate = 0;

		if(isOverdue()){
			Calendar cal = Calendar.getInstance();
			Date today = cal.getTime();
			long startDateTime = dueDate.getTime();
		    long endDateTime = today.getTime();
		    long millisecondsPerDay = 1000*60*60*24; 
		    daysLate = (int) ((endDateTime - startDateTime) / millisecondsPerDay);
		}
		
		return daysLate;
	}
	
	/**
	 * Returns whether or not the book is overdue
	 * @return
	 */
	public synchronized boolean isOverdue(){
		Calendar cal = Calendar.getInstance();
		Date today = cal.getTime();
		if(today.after(dueDate)){
			return true;
		}
		return false;
	}
	
}
